Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Introduction to Array in Python
Python - Array Methods
Quiz
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python Comments

Comments are crucial in programming as they help explain and clarify the code to users and your future self. They are used to annotate parts of the code with notes or explanations and are not executed as part of the program. Python supports several types of comments:

  • single-line
  • multi-line

Each serves different purposes, as we'll explore below.

Single-line Comments

Single-line comments are used to comment out a small piece of information or annotate a line of code. You can use # symbol to add single line comment in the python code.

Example

Python3

. . . .

Explanation:

  • The # symbol marks the start of a comment. Python will ignore the text following the # on the same line. This allows you to write notes or disable code temporarily.
  • In the first line, the comment is used to describe the purpose of the code.
  • The second line includes a comment right after the code, explaining what that specific line of code does.

Multi-line Comments

Python does not have a specific multi-line comment feature like some other languages do, but you can use consecutive single-line comments or a multi-line string (using triple quotes) that isn't assigned to a variable.

Example

Python3

. . . .

Explanation:

  • The first two lines show consecutive single-line comments, each starting with #.
  • The block enclosed in triple quotes (""") is technically a multi-line string, but when not assigned to a variable or used in a function or class, it acts like a comment. Python will read these lines but not execute them, similar to comments.

By using comments effectively, you can make your code much easier to understand and maintain. Whether it’s a simple script or a complex project, well-commented code can save time and effort for anyone who might need to read or modify it in the future.

Python - Basic Syntax
Quiz
Mark as Completed

On this page

Single-line Comments

Example

Multi-line Comments

Example